|
|
|
|
|
|
|
After the DAO reference has been set up, you can then open a database by using the Workspace object's OpenDatabase method. The terminology gets a little confusing here: To create a Database object in your program, you open an existing database. The OpenDatabase method is technically a method of a Workspace object, but the default Workspace object DBEngine.Workspaces(0) is assumed and can be omitted. Therefore, assuming a project contains only one Workspace object, these lines of code are equivalent: |
|
|
|
|
|
|
|
|
Set dbTest = DBEngine.Workspaces(0).OpenDatabase("BIBLIO.MDB")
Set dbTest = OpenDatabase("BIBLIO.MDB") |
|
|
|
|
|
|
|
|
Throughout this chapter, you'll work on a application that opens the sample database BIBLIO.MDB, which is included with Visual Basic. Follow these steps to begin the sample application: |
|
|
|
|
|
|
|
|
1. Start Visual Basic, if necessary, and create a new Standard EXE project. |
|
|
|
|
|
|
|
|
2. Change the name of Form1 to frmDAOTest, and save the project. |
|
|
|
|
|
|
|
|
3. To add a reference to the proper DAO library to the project, choose Project, References. The References ProjectName dialog box appears (refer to Figure 26.2). |
|
|
|
|
|
|
|
|
4. Scroll down to Microsoft DAO 3.51 Object Library and select the check box next to it. |
|
|
|
|
|
|
|
|
5. Click OK to close the dialog box. |
|
|
|
|
|
|
|
|
Once you have completed these steps, your project contains a reference to the Data Access Objects library. You therefore can use DAO in the application. |
|
|
|
|
|
|
|
|
Opening an Existing Database |
|
|
|
|
|
|
|
|
The first step in writing many data access programs is to set up a link to the database with which you want to work. If your application will work with a database that already exists, you need to create a Database object within the program and then use that object to create a link to the existing database. In effect, you are "opening the database" for use in your program. Most of the other Data Access Objects flow from that Database object. |
|
|
|
|
|
|
|
|
Think of the Database object as your Visual Basic program's internal representation of a physical database, not the database itself. The Database object within the program is simply a window into the database. |
|
|
|
|
|
|
|
|
Continuing the sample application, the following steps use the OpenDatabase method to create a Database object and set it up to work with the BIBLIO.MDB database: |
|
|
|
|
|
|
|
|
1. In the form's General Declarations section, add this code to declare a form-level Database object variable: |
|
|
|
|
|
|
|
|
2. Add the following lines of code to the form's Load event procedure. This code, which executes as the form loads, creates the Database object dbTest by opening the BIBLIO.MDB database: |
|
|
|
|
|